当DP遇见Py(十九) -- 命令模式

定义:

将一个请求封装为一个对象,从而使可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。

类图:

类型:行为型

实例:

烧烤店客户向服务员点单,服务员将点好的单告诉大厨,由大厨进行烹饪。

C++ 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <list>

using namespace std;

class Barbecuer
{
public:
void bakeMutton()
{

cout<<"烤羊肉串"<<endl;
}
void bakeChickenWing()
{

cout<<"烤鸡翅"<<endl;
}
};

class Command
{
protected:
Barbecuer *receiver;
public:
Command(Barbecuer *receiver)
{
this->receiver=receiver;
}
virtual void executeCommand()=0;
};

class BakeMuttonCommand:public Command
{
public:
BakeMuttonCommand(Barbecuer *receiver):Command(receiver)
{}
void executeCommand()
{

receiver->bakeMutton();
}
};

class BakeChikenWingCommand:public Command
{
public:
BakeChikenWingCommand(Barbecuer *receiver):Command(receiver)
{}
void executeCommand()
{

receiver->bakeChickenWing();
}
};

class Waiter
{
private:
list<Command*> orders;
public:
void setOrder(Command *command)
{

orders.push_back(command);
}

void notify()
{

list<Command*>::iterator iter=orders.begin();
while(iter!=orders.end())
{
(*iter)->executeCommand();
iter++;
}
}
};

int main()
{


Barbecuer *boy=new Barbecuer();
Command *bm1=new BakeMuttonCommand(boy);
Command *bm2=new BakeMuttonCommand(boy);
Command *bc1=new BakeChikenWingCommand(boy);

Waiter *girl=new Waiter();

girl->setOrder(bm1);
girl->setOrder(bm2);
girl->setOrder(bc1);

girl->notify();

return 0;
}

Python 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding=utf-8 -*-

class Barbecuer(object):
def bakeMutton(self):
print u'烤羊肉串'
def bakeChickenWing(self):
print u'烤鸡翅'

class Command(object):
def __init__(self, receiver):
self.receiver = receiver
def execute(self):
raise NotImplementedError()

class BakeMuttonCommand(Command):
def __init__(self, receiver):
super(BakeMuttonCommand, self).__init__(receiver)
def execute(self):
self.receiver.bakeMutton()

class BakeChikenWingCommand(Command):
def __init__(self, receiver):
super(BakeChikenWingCommand, self).__init__(receiver)
def execute(self):
self.receiver.bakeChickenWing()

class Waiter(object):
def __init__(self):
self.commands = []
def setOrder(self, command):
self.commands.append(command)
def notify(self):
for command in self.commands:
command.execute()

if __name__ == '__main__':
boy = Barbecuer()
bm1 = BakeMuttonCommand(boy)
bm2 = BakeMuttonCommand(boy)
bc1 = BakeChikenWingCommand(boy)

girl = Waiter()

girl.setOrder(bm1)
girl.setOrder(bm2)
girl.setOrder(bc1)

girl.notify()

执行结果:

1
2
3
烤羊肉串
烤羊肉串
烤鸡翅

Tips:

命令模式Python实现没有什么特点,在这就不在赘述了。

评论